﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web;
using AL.Entity;

namespace AL.Helper
{

    public class SogoAccountAnalizer
    {
        private class DocumentAnylizer
        {
            private readonly string html;
            private static readonly Regex WeixinnameRegex = new Regex("<h3 id=\"weixinname\">(.*)</h3>", RegexOptions.Compiled);
            private static readonly Regex WeixinidRegex = new Regex("<span>微信号：(.*)</span>", RegexOptions.Compiled);
            private static readonly Regex IntroductionRegex = new Regex("<span class=\"sp-tit\">功能介绍:</span><span class=\"sp-txt\">(.*)</span>", RegexOptions.Compiled);
            private static readonly Regex AuthenticatedFlgRegex = new Regex("<span class=\"ico-r\"></span>", RegexOptions.Compiled);
            private static readonly Regex AuthenticationContextRegex = new Regex("认证：</span><span class=\"sp-txt\">(.*)</span>", RegexOptions.Compiled);
            private static readonly Regex HeaderIconURLRegex = new Regex("<a href=\"#\" onclick=\"return false;\">.*src=\"([^\"]*)\".*</a>", RegexOptions.Compiled);
            private static readonly Regex QRCodeIconURLRegex = new Regex("<img style=\"width:34px;height:34px\" src=\"([^\"]*)\"", RegexOptions.Compiled);
            private static readonly Regex QRCodePictureURLRegex = new Regex("</em><img width=\"140\" height=\"140\" alt=\"\" src=\"([^\"]*)\"", RegexOptions.Compiled);

            public DocumentAnylizer(string htmlDocument)
            {
                this.html = htmlDocument;
            }

            public Account GetAccount()
            {
                var account = new Account();
                var match = WeixinnameRegex.Match(this.html);
                if (match.Success)
                {
                    account.Name = match.Groups[1].Value;
                }
                match = WeixinidRegex.Match(this.html);
                if (match.Success)
                {
                    account.Id = match.Groups[1].Value;
                }
                match = IntroductionRegex.Match(this.html);
                if (match.Success)
                {
                    account.Introduction = HttpUtility.HtmlDecode(match.Groups[1].Value);
                }
                match = AuthenticatedFlgRegex.Match(this.html);
                account.AuthenticatedFlg = match.Success;
                match = AuthenticationContextRegex.Match(this.html);
                if (match.Success)
                {
                    account.AuthenticationContext = match.Groups[1].Value;
                }
                match = HeaderIconURLRegex.Match(this.html);
                if (match.Success)
                {
                    account.HeaderIconURL = match.Groups[1].Value;
                }
                match = QRCodeIconURLRegex.Match(this.html);
                if (match.Success)
                {
                    account.QRCodeIconURL = match.Groups[1].Value;
                }
                match = QRCodePictureURLRegex.Match(this.html);
                if (match.Success)
                {
                    account.QRCodePictureURL = match.Groups[1].Value;
                }
                return account;
            }
        }



        public string ActicleHistoryURLPrefix { get; set; }

        // 对象初始化
        public SogoAccountAnalizer()
        {
            this.ActicleHistoryURLPrefix = "http://weixin.sogou.com/gzh?openid=";
        }

        public Account GetAccount(string url)
        {
            var webClient = new WebClient { Encoding = Encoding.UTF8 };
            string htmlDocument = webClient.DownloadString(url);
            var documentAnylizer = new DocumentAnylizer(htmlDocument);
            var account = documentAnylizer.GetAccount();

            Account result;
            if (account == null)
            {
                result = null;
            }
            else
            {
                account.OpenId = url.Substring(this.ActicleHistoryURLPrefix.Length);
                account.Provider = "搜狗";
                result = account;
            }
            return result;
        }

    }
}
